package demoMod.invincibleOne.cards.invincible;

import basemod.helpers.CardModifierManager;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.rooms.MonsterRoom;
import com.megacrit.cardcrawl.rooms.RestRoom;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.interfaces.EnterRoomHook;
import demoMod.invincibleOne.interfaces.SpawnMonsterHook;
import demoMod.invincibleOne.modifiers.ExhaustAtStartOfCombatMod;
import demoMod.invincibleOne.relics.TaskGoal;

public class RoadCones extends AbstractInvincibleCard implements EnterRoomHook, SpawnMonsterHook {
    public static final String ID = InvincibleOneMod.makeID("RoadCones");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/RoadCones.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.COMMON;
    private static final CardTarget TARGET = CardTarget.NONE;

    private static final int COST = -2;

    private boolean triggered;

    public RoadCones() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.modifier = new ExhaustAtStartOfCombatMod();
        CardModifierManager.addModifier(this, modifier);
        this.tags.add(CardTagsEnum.ALERT);
        this.baseMagicNumber = this.magicNumber = 15;
    }

    @Override
    public boolean onBattleStart() {
        this.triggered = this.misc > 0;
        if (triggered) {
            TaskGoal.AddToAlertQueue(30, new TaskGoal.AlertLambda() {
                @Override
                public void run() {
                    for (AbstractMonster m : AbstractDungeon.getMonsters().monsters) {
                        addToTop(new AbstractGameAction() {
                            @Override
                            public void update() {
                                if (m.maxHealth >= 100 && AbstractDungeon.getCurrRoom().getClass().equals(MonsterRoom.class)) {
                                    m.increaseMaxHp(magicNumber * 2, true);
                                } else {
                                    m.increaseMaxHp(magicNumber, true);
                                }
                                isDone = true;
                            }
                        });
                    }
                }
            });
        }
        if (this.misc < 0) {
            this.misc = 0;
        }
        return (this.misc--) > 0;
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> upgradeMagicNumber(5);
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {

    }

    @Override
    public void onEnterRoom(AbstractRoom room) {
        if (room instanceof RestRoom) {
            this.misc = 1;
        }
    }

    @Override
    public void onSpawnMonster(AbstractMonster m) {
        if (triggered) {
            TaskGoal.AddToAlertQueue(30, new TaskGoal.AlertLambda() {
                @Override
                public void run() {
                    addToTop(new AbstractGameAction() {
                        @Override
                        public void update() {
                            if (m.maxHealth >= 100 && AbstractDungeon.getCurrRoom().getClass().equals(MonsterRoom.class)) {
                                m.increaseMaxHp(magicNumber * 2, true);
                            } else {
                                m.increaseMaxHp(magicNumber, true);
                            }
                            isDone = true;
                        }
                    });
                }
            });
        }
    }
}
